using Assets.Scripts.GDK;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Brick
{
	public class BallBox : gdk_base_UI
	{
		public Transform ball;

		public Rigidbody2D[] allBall;

		public List<GameObject> allBalls;

		public GameObject balls;

		public int curBallNums = 50;

		public int curAddBallNums;

		public int curlimitBall;

		public float shootingSpeed = 20f;

		public bool gameOver;

		public bool curShoting;

		public bool gameStart;

		public Vector3 muzzlePos;

		private Coroutine sendBalls;

		private Coroutine tickCor;

		private float dirTime = 0.05f;

		private float countTime;

		private int sendNum;

		private bool isShooting;

		private Vector2 dirPos;

		public override void OnLoad()
		{
			gdk_game_manager.AddEventListener<int>(EventID.GAME_CREATE_BALL, CreatBall);
			gdk_game_manager.AddEventListener<int>(EventID.GAME_ADD_BALL, AddBall);
			gdk_game_manager.AddEventListener<int>(EventID.GAME_LIT_ADD_BALL, onLitAddBall);
			gdk_game_manager.AddEventListener<Vector2>(EventID.GAME_BALL_FORCE, OnAddForce);
			gdk_game_manager.AddEventListener(EventID.GAME_ALLBALL_BACK, OnAllBallBack);
			gdk_game_manager.AddEventListener(EventID.GAME_SHOOP_START, OnShoot);
			gdk_game_manager.AddEventListener(EventID.BALLCHANGE, OnChangeBall);
		}

		public override void UnLoad()
		{
			gdk_game_manager.RemoveEventListener<int>(EventID.GAME_CREATE_BALL, CreatBall);
			gdk_game_manager.RemoveEventListener<int>(EventID.GAME_ADD_BALL, AddBall);
			gdk_game_manager.RemoveEventListener<int>(EventID.GAME_LIT_ADD_BALL, onLitAddBall);
			gdk_game_manager.RemoveEventListener<Vector2>(EventID.GAME_BALL_FORCE, OnAddForce);
			gdk_game_manager.RemoveEventListener(EventID.GAME_ALLBALL_BACK, OnAllBallBack);
			gdk_game_manager.RemoveEventListener(EventID.GAME_SHOOP_START, OnShoot);
			gdk_game_manager.RemoveEventListener(EventID.BALLCHANGE, OnChangeBall);
		}

		public override void OnStart()
		{
			muzzlePos = getMuzzle();
			allBalls = new List<GameObject>();
			string pathName = "Prefabs/ball 1";
			EnemyPool.GetInstance().CreateObj(80, "ball 1", pathName, base.transform);
		}

		public override void OnUpdate()
		{
			if (isShooting)
			{
				countTime += Time.deltaTime;
				if (countTime > dirTime)
				{
					shootBall();
					countTime = 0f;
				}
				if (sendNum >= curBallNums + curAddBallNums)
				{
					isShooting = false;
				}
			}
		}

		private void shootBall()
		{
			GameObject gameObject = allBalls[sendNum];
			if ((bool)gameObject)
			{
				sendNum++;
				int num = curBallNums + curAddBallNums;
				int ballSpeed = getBallSpeed();
				if (gameObject.GetComponent<BallMove>().state == BallState.Ready)
				{
					gameObject.GetComponent<BallMove>().state = BallState.Battle;
					gameObject.GetComponent<CircleCollider2D>().isTrigger = false;
					gameObject.GetComponent<Rigidbody2D>().velocity = dirPos.normalized * ballSpeed;
					SendOutBall();
					List<int> list = new List<int>();
					list.Add(2);
					list.Add(num - sendNum);
					gdk_game_manager.DispachEvent(EventID.GAME_BALL_NUM_CHANGE, list);
				}
			}
		}

		public void RemoveAllChild()
		{
			isShooting = false;
			for (int num = allBalls.Count - 1; num >= 0; num--)
			{
				GameObject gameObject = allBalls[num];
				gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
				allBalls.RemoveAt(num);
				EnemyPool.GetInstance().RecycleObj(gameObject);
			}
		}

		private void CreatBall(int nums)
		{
			RemoveAllChild();
			muzzlePos = getMuzzle();
			curAddBallNums = 0;
			curBallNums = nums;
			for (int i = 0; i < curBallNums; i++)
			{
				GameObject obj = EnemyPool.GetInstance().GetObj("ball 1", "Prefabs/effect/ball 1");
				Transform transform = obj.transform;
				transform.SetParent(base.transform);
				transform.localScale = Vector3.one;
				transform.localPosition = new Vector2(0f, muzzlePos.y);
				transform.gameObject.layer = 9;
				obj.SetActive(value: true);
				transform.GetComponent<BallMove>().state = BallState.Ready;
				allBalls.Add(transform.gameObject);
			}
			GDK.ModuleManger.Module<BallFindWayModule>().SetWordShootPos(allBalls[0].transform.position);
			GDK.ModuleManger.Module<BallFindWayModule>().SetShootPos(allBalls[0].transform.localPosition);
			Vector2 obj2 = allBalls[0].transform.position;
			gdk_game_manager.DispachEvent(EventID.GAME_BALL_POS_CHANGE, obj2);
			SetBalls();
			SetAddBalls();
			List<int> list = new List<int>();
			list.Add(1);
			list.Add(curBallNums);
			gdk_game_manager.DispachEvent(EventID.GAME_BALL_NUM_CHANGE, list);
		}

		private void onLitAddBall(int nums)
		{
			curlimitBall += nums;
		}

		private void AddBall(int nums)
		{
			curAddBallNums += nums;
			Transform component = balls.GetComponent<Transform>();
			for (int i = 0; i < nums; i++)
			{
				GameObject obj = EnemyPool.GetInstance().GetObj("ball 1", "Prefabs/effect/ball 1");
				Transform transform = obj.transform;
				transform.SetParent(component);
				transform.position = allBalls[0].GetComponent<Transform>().position;
				transform.gameObject.layer = 9;
				transform.GetComponent<BallMove>().state = BallState.Ready;
				allBalls.Add(transform.gameObject);
			}
			SetAddBalls();
			List<int> list = new List<int>();
			list.Add(1);
			list.Add(curBallNums + curAddBallNums);
			gdk_game_manager.DispachEvent(EventID.GAME_BALL_NUM_CHANGE, list);
		}

		private void OnAddForce(Vector2 direction)
		{
			Time.timeScale = 1f;
			dirPos = direction;
			sendNum = 0;
			CleanSendOut();
			isShooting = true;
		}

		private IEnumerator TickSend()
		{
			int tickCount = 0;
			bool bFast = false;
			for (int i = 0; i < 30; i++)
			{
				yield return new WaitForSeconds(1f);
				tickCount++;
				if (!bFast && tickCount > 3)
				{
					bFast = true;
					gdk_game_manager.DispachEvent(EventID.GAME_QUICKBG, 0);
				}
			}
		}

		private IEnumerator LineLaunch(Vector2 direction)
		{
			int curCount = curBallNums + curAddBallNums;
			int ballSpeed = getBallSpeed();
			for (int i = 0; i < allBalls.Count; i++)
			{
				yield return new WaitForSeconds(0.05f);
				allBalls[i].GetComponent<BallMove>().state = BallState.Battle;
				allBalls[i].GetComponent<CircleCollider2D>().isTrigger = false;
				allBalls[i].GetComponent<Rigidbody2D>().velocity = direction.normalized * ballSpeed;
				SendOutBall();
				curCount--;
				gdk_game_manager.DispachEvent<List<int>>(obj: new List<int>
			{
				2,
				curCount
			}, name: EventID.GAME_BALL_NUM_CHANGE);
			}
		}

		public Vector3 getMuzzlePos()
		{
			return muzzlePos;
		}

		private Vector3 getMuzzle()
		{
			return GDK.ModuleManger.Module<BallFindWayModule>().getShootPos();
		}

		private Vector2 getWordMuzzle()
		{
			return GDK.ModuleManger.Module<BallFindWayModule>().GetWordShootPos();
		}

		private bool IsBBack()
		{
			return GDK.ModuleManger.Module<BallBoxModule>().GetBallsBack();
		}

		private void SetBBack(bool bBack)
		{
			GDK.ModuleManger.Module<BallBoxModule>().SetBallBack(bBack);
		}

		private void SendOutBall()
		{
			GDK.ModuleManger.Module<BallBoxModule>().OnSendOut();
		}

		private void CleanSendOut()
		{
			GDK.ModuleManger.Module<BallBoxModule>().CleanSendOut();
		}

		private void SetBalls()
		{
			GDK.ModuleManger.Module<BallBoxModule>().SetBallNum(curBallNums);
		}

		private void SetAddBalls()
		{
			GDK.ModuleManger.Module<BallBoxModule>().SetAddBallNum(curAddBallNums);
		}

		private int getBallSpeed()
		{
			return GDK.ModuleManger.Module<BallBoxModule>().getBallSpeed();
		}

		public void OnAllBallBack()
		{
			isShooting = false;
			if (sendBalls != null)
			{
				StopCoroutine(sendBalls);
				sendBalls = null;
			}
			SetBBack(bBack: true);
			Vector2 wordMuzzle = getWordMuzzle();
			int ballSpeed = getBallSpeed();
			for (int i = 0; i < allBalls.Count; i++)
			{
				switch (allBalls[i].GetComponent<BallMove>().state)
				{
					case BallState.Ready:
						allBalls[i].GetComponent<BallMove>().state = BallState.BACK;
						allBalls[i].GetComponent<Rigidbody2D>().velocity = Vector2.left;
						continue;
					case BallState.Bore:
					case BallState.SLEEP:
						continue;
				}
				allBalls[i].GetComponent<BallMove>().state = BallState.BACK;
				if (allBalls[i].GetComponent<Rigidbody2D>().velocity != Vector2.zero)
				{
					allBalls[i].GetComponent<CircleCollider2D>().isTrigger = true;
					Vector2 vector = wordMuzzle - allBalls[i].GetComponent<Rigidbody2D>().position;
					allBalls[i].GetComponent<Rigidbody2D>().velocity = vector.normalized * ballSpeed;
				}
				else
				{
					allBalls[i].GetComponent<Rigidbody2D>().position = wordMuzzle;
				}
			}
		}

		public void OnShoot()
		{
			OnChangeBall();
			if (curlimitBall > 0)
			{
				AddBall(curlimitBall);
				curlimitBall = 0;
			}
			List<int> list = new List<int>();
			list.Add(1);
			list.Add(curBallNums + curAddBallNums);
			gdk_game_manager.DispachEvent(EventID.GAME_BALL_NUM_CHANGE, list);
		}

		public void OnChangeBall()
		{
			for (int i = 0; i < allBalls.Count; i++)
			{
				allBalls[i].GetComponent<BallMove>().showBallBg();
				allBalls[i].GetComponent<BallMove>().state = BallState.Ready;
			}
		}
	}
}

